package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Pedro Passos e CIA
	 */
	public class Jogo extends MovieClip
	{
		private var level:Level;
		
		private var heroi:Heroi;
		
		var gravidade:Number = 0.9;
		
		var velocidadeY:Number = 0;
		
		var BO_pulando:Boolean;
		var BO_esq:Boolean;
		var BO_dir:Boolean;
		
		public function Jogo()
		{
			
			Main.CleanScreen();
			DrawScreen();
		}
		
		private function DrawScreen():void
		{
			level = new Level();
			level.currentLevel = 1;
			
			
			heroi = new Heroi();
			heroi.x = 200;
			heroi.y = Main.stage.stageHeight - heroi.height / 2;
			Main.screen.addChild(heroi);
			
			addEventListener(Event.ENTER_FRAME, update, false, 0, true)
			Main.stage.addEventListener(KeyboardEvent.KEY_DOWN, pressionar, false, 0, true)
			Main.stage.addEventListener(KeyboardEvent.KEY_UP, soltar, false, 0, true)
		}
		
		public function update(e:Event):void
		{
			//trace(level.fundo.x);
			if (BO_esq == true)
			{
				if (heroi.x >= 0)
				{
					level.fundo.x += 5;
				}
				else
				{
					heroi.x -= 5;
				}
			}
			
			if (BO_dir == true)
			{
				if (heroi.x >= Main.stage.stageWidth * 2 / 3)
				{
					
					if (level.fundo.x <= -level.fundo.width + level.fundo.width / 2)
					{
						level.fundo.x = -level.fundo.width + level.fundo.width / 2;
						heroi.x += 5;
						if (heroi.x >= Main.stage.stageWidth - heroi.width / 2)
						{
							heroi.x = Main.stage.stageWidth - heroi.width / 2;
						}
					}
					else
					{
						level.fundo.x -= 5;
					}
				}
				else
				{
					heroi.x += 5;
				}
				
			}
			
			if (BO_esq == true)
			{
				
				if (heroi.x <= Main.stage.stageWidth / 3)
				{
					
					if (level.fundo.x >= 0)
					{
						level.fundo.x = 0;
						heroi.x -= 5;
						
						if (heroi.x <= 0 + heroi.width / 2)
						{
							heroi.x = 0 + heroi.width / 2;
						}
					}
					else
					{
						level.fundo.x += 5;
					}
					
				}
				else
				{
					heroi.x -= 5;
				}
				
			}
			
			if (heroi.y >= Main.stage.stageHeight - heroi.height / 2)
			{
				heroi.y = Main.stage.stageHeight - heroi.height / 2
				velocidadeY = 0;
				BO_pulando = false;
			}
		
		}
		
		function pressionar(e:KeyboardEvent):void
		{
			switch (e.keyCode)
			{
				case Keyboard.LEFT: 
					BO_esq = true;
					break;
				
				case Keyboard.RIGHT: 
					BO_dir = true;
					break;
			}
			
			if (BO_pulando == false)
			{
				switch (e.keyCode)
				{
					case Keyboard.SPACE: 
						velocidadeY -= 10;
						BO_pulando = true;
						break;
				}
			}
		}
		
		function soltar(e:KeyboardEvent)
		{
			switch (e.keyCode)
			{
				case Keyboard.LEFT: 
					BO_esq = false;
					break;
				
				case Keyboard.RIGHT: 
					BO_dir = false;
					break;
			}
		}
	
	}
}